﻿using FineUI;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZHDJ.BranchCenter.BLL;
using ZHDJ.Data;
using ZHDJ.Data.Models.MassLine;

namespace ZHDJ.BranchCenter.Module.MassLine
{
    public partial class FeedbackList : PageBase
    {
        #region 
        /// <summary>
        /// 获取数据源
        /// </summary>
        public DataSet DataSource
        {
            get
            {
                //string ddlTypeText = this.ddlType.Items[ddlFeedBackTo.SelectedIndex].Text.Trim();
                //string ddlTypeOptionText = this.ddlTypeOption.Items[ddlTypeOption.SelectedIndex].Text.Trim();
                string ddlTypeText = "";
                string ddlTypeOptionText = "";
                if (ppBranch.Value == "")
                {
                    ddlTypeText = "";
                }
                if (ddlTypeOption.SelectedValue == "")
                {
                    ddlTypeOptionText = "";
                }
                if (hdJudge.Value == "true")
                {
                    return PartyMassLineFeedback.GetGridData(Grid1.PageIndex, Grid1.PageSize, this.ppBranch.Value, ddlTypeText, ddlTypeOptionText);
                }
                else
                {
                    return PartyMassLineFeedback.GetGridData(Grid1.PageIndex, Grid1.PageSize, this.ppBranch.Value, ddlTypeText);
                }
            }
        }
        #endregion

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "";
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
                BindData();
            }
            if (ddlType.SelectedValue == "0")
            {
                this.ddlTypeOption.Hidden = false;
                hdJudge.Value = "true";
            }
            else
            {
                this.ddlTypeOption.Hidden = true;
                hdJudge.Value = "false";
            }
            if (ddlTypeOption.SelectedValue == "")
            {
                hdJudge.Value = "false";
            }
        }

        private void LoadData()
        {
            // 每页记录数
            Grid1.PageSize = ConfigHelper.PageSize;

            BindGrid();
        }

        private void BindData()
        {
        }
        private void BindGrid()
        {
            var ds = this.DataSource;

            Grid1.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
            Grid1.DataSource = ds.Tables[0];
            Grid1.DataBind();
        }

        protected void Grid1_PreDataBound(object sender, EventArgs e)
        {
            // 数据绑定之前，进行权限检查
            //CheckPowerWithWindowField("PartyEduHonourEdit", Grid1, "editField");
            //CheckPowerWithLinkButtonField("PartyEduHonourDelete", Grid1, "deleteField");
        }

        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            Grid1.SortDirection = e.SortDirection;
            Grid1.SortField = e.SortField;
            BindGrid();
        }

        protected void ddlGridPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlGridPageSize.SelectedValue);
            BindGrid();
        }

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }
        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            string IDs = GetSelectedDataKeyID(Grid1);

            if (e.CommandName == "Delete")
            {
                // 在操作之前进行权限检查
                //if (!CheckPower("PartyEduHonourDelete"))
                //{
                //    CheckPowerFailWithAlert();
                //    return;
                //}

                var del = DB.PartyMassLineFeedbacks.FirstOrDefault(t => t.ID == IDs);
                DB.PartyMassLineFeedbacks.Remove(del);
                DB.SaveChanges();
                BindGrid();
            }
        }

        #region 按钮事件
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindGrid();
        }
        protected void btnDeleteSelected_Click(object sender, EventArgs e)
        {
            // 在操作之前进行权限检查
            //if (!CheckPower("PartyEduHonourDelete"))
            //{
            //    CheckPowerFailWithAlert();
            //    return;
            //}

            // 从每个选中的行中获取ID（在Grid1中定义的DataKeyNames）
            List<string> ids = GetSelectedDataKeyIDs(Grid1);

            // 执行数据库操作
            DB.PartyMassLineFeedbacks.Where(u => ids.Contains(u.ID)).ToList().ForEach(u => DB.PartyMassLineFeedbacks.Remove(u));
            DB.PartyMasslineFeedbackOptions.Where(u => ids.Contains(u.PID)).ToList().ForEach(u => DB.PartyMasslineFeedbackOptions.Remove(u));

            DB.SaveChanges();

            // 重新绑定表格
            BindGrid();
        }
        #endregion
    }
}